package com.handinfo.android.uicontrols.controls;

import java.util.concurrent.CopyOnWriteArrayList;

import android.graphics.PointF;
import android.view.MotionEvent;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.core.graphics.DWGraphics;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.uicontrols.DWGuideObject;

/**
 * 主窗口控件
 * @author handinfo
 */
public class DWFrame extends DWControl
{

	public boolean m_null_frame = true;

	private int m_frame_color = 0x8800504B;
	public static final byte FRAME_TYPE_FULL = 0;//全背景
	public static final byte FRAME_TYPE_HALF_LEFT = 1;//左半边
	public static final byte FRAME_TYPE_HALF_RIGHT = 2;//右半边
	public static final byte FRAME_TYPE_HALF_HCENTER = 3;//居中
	
	private boolean m_through = false;//是否传递给后面的Frame
	private boolean m_click_close = true;//点击其他地方是否关闭
	private boolean m_shield = false;//点击自身区域内是否传递给后面的Frame
	
	private int m_zoom_size = 50;
	public float m_click_x = 0;
	public float m_click_y = 0;

	private CopyOnWriteArrayList<DWControl> m_controls = new CopyOnWriteArrayList<DWControl>();

	public DWFrame()
	{
		m_null_frame = true;
		setTouchZoomIn(m_zoom_size, m_zoom_size);
	}

	public DWFrame(byte type)
	{
		this(type, true);
		setTouchZoomIn(m_zoom_size, m_zoom_size);
	}

	public DWFrame(byte type, boolean background)
	{
		switch (type) {
			case FRAME_TYPE_FULL:
				setShowWideHigh(DWControlsManager.CONTROL_WIDTH - 40, DWControlsManager.CONTROL_HEIGHT - 60);
				setCentre();
				break;
			case FRAME_TYPE_HALF_LEFT:
				setShowWideHigh(DWControlsManager.CONTROL_WIDTH / 2 - 30, DWControlsManager.CONTROL_HEIGHT - 60);
				setPoint((DWGameManager.Screen_Width / 2 - m_show_w) / 2, (DWGameManager.Screen_Height - m_show_h) / 2);
				setThrough(true);
				break;
			case FRAME_TYPE_HALF_RIGHT:
				setShowWideHigh(DWControlsManager.CONTROL_WIDTH / 2 - 30, DWControlsManager.CONTROL_HEIGHT - 60);
				setPoint(DWGameManager.Screen_Width / 2 + (DWGameManager.Screen_Width / 2 - m_show_w) / 2, (DWGameManager.Screen_Height - m_show_h) / 2);
				setThrough(true);
				break;
			case FRAME_TYPE_HALF_HCENTER:
				setShowWideHigh(DWControlsManager.CONTROL_WIDTH / 2 - 40, DWControlsManager.CONTROL_HEIGHT - 60);
				setCentre();
				break;
		}
		if (background) {
			m_null_frame = !background;
		}
		setTouchZoomIn(m_zoom_size, m_zoom_size);
	}

	public DWFrame(int w, int h)
	{
		setPoint((DWGameManager.Screen_Width - w) >> 1, (DWGameManager.Screen_Height - h) >> 1);
		setShowWideHigh(w, h);
		m_null_frame = false;
		setTouchZoomIn(m_zoom_size, m_zoom_size);
	}

	public DWFrame(int x, int y, int w, int h)
	{
		setPoint(x, y);
		setShowWideHigh(w, h);
		m_null_frame = false;
		setTouchZoomIn(m_zoom_size, m_zoom_size);
	}
	
	/**
	 * 设置触摸传递给后面的Frame,true为传递,false为不传递
	 * @param through
	 */
	public void setThrough(boolean through) {
		m_through = through;
	}
	
	/**
	 * 点击自身区域内是否传递给下一层Frame
	 * @param shield
	 */
	public void setShield(boolean shield) {
		m_shield = shield;
	}
	
	/**
	 * 设置点击其他区域是否关闭此Frame,true为关闭
	 * @param close
	 */
	public void setClickClose(boolean close) {
		m_click_close = close;
	}

	public void addControl(DWControl control)
	{
		if (control != null) {
			control.setID(m_controls.size());
			int size = m_controls.size();
			if (size > 0) {
				DWControl temp = null;
				if (!control.getTop()) {
					for (int i = size - 1; i > -1; i--) {
						temp = m_controls.get(i);
						if (!temp.getTop()) {
							m_controls.add(i + 1, control);
							return;
						}
					}
				}
			}
			m_controls.add(control);
		}
	}

	public void removeControl(DWControl control)
	{
		if (control != null) {
			m_controls.remove(control);
		}
	}
	
	public boolean contains(DWControl control) {
		if (control != null) {
			return m_controls.contains(control);
		}
		return false;
	}

	public void setBackgroundColor(int color)
	{
		m_frame_color = color;
	}

	public void render(DWGraphics g)
	{
		if (!m_null_frame) {
			g.setClip(m_show_x, m_show_y, m_show_w, m_show_h);
			g.setColor(m_frame_color);
			g.fillRect(m_show_x, m_show_y, m_show_w, m_show_h);
		}
		DWControlsManager cm = DWControlsManager.getInstance();
		if (cm != null) {
			cm.m_guide_control = null;
		}
		DWControl control;
		for (int i = 0; i < m_controls.size(); i++) {
			control = m_controls.get(i);
			if (control.m_is_show) {
				control.render(g);
			}
		}
		DWGuideObject guide = DWControlsManager.getInstance().m_guides.getCurrentGuideObject();
		if (guide != null && cm != null && cm.m_guide_control != null) {
			guide.draw(g, cm.m_guide_x, cm.m_guide_y, cm.m_guide_w, cm.m_guide_h);
		}
		
		renderRect(g);
	}

	public void logic()
	{
		DWControl deal = null;
		for (int i = 0; i < m_controls.size(); i++) {
			m_controls.get(i).logic();
			if (m_controls.get(i).getDeal()) {
				deal = m_controls.get(i);
			}
		}
		if (deal != null) {
			deal.destroy();
			m_controls.remove(deal);
			deal = null;
		}
	}
	
	public void destroy() {
		if (m_controls != null) {
			for (int i = 0; i < m_controls.size(); i++) {
				DWControl control = m_controls.remove(i);
				if (control != null) {
					control.destroy();
					control = null;
				}
			}
		}
	}

	public boolean doLongPress(PointF point)
	{
		if (checkPointOnRect(point.x, point.y)) {
			if (m_long_listener != null) {
				m_click_x = point.x;
				m_click_y = point.y;
				m_long_listener.OnLong(true);
			}
			for (int i = m_controls.size() - 1; i > -1; i--) {
				if (m_controls.get(i).doLongPress(point)) {
					return true;
				}
				if (DWControlsManager.isMatte(m_controls.get(i))) {
					break;
				}
			}
			if (!m_shield) {
				return true;
			}
		}
		if (!m_through) {
			return true;
		}
		return false;
	}
	
	public boolean doClick(float x, float y)
	{
		if (checkPointOnRect(x, y)) {
			for (int i = m_controls.size() - 1; i > -1; i--) {
				if (m_controls.get(i).doClick(x, y)) {
					return true;
				}
				if (DWControlsManager.isMatte(m_controls.get(i))) {
					break;
				}
			}
			if (!m_shield) {
				return true;
			}
		} else {
			if (m_click_close) {
				destruct();
				return true;
			}
		}
		if (!m_through) {
			return true;
		}
		return false;
	}

	public boolean doLongScroll(PointF start, PointF end, float distanceX, float distanceY)
	{
		if (checkPointOnRect(end.x, end.y)) {
			for (int i = m_controls.size() - 1; i > -1; i--) {
				if (m_controls.get(i).doLongScroll(start, end, distanceX, distanceY)) {
					return true;
				}
				if (DWControlsManager.isMatte(m_controls.get(i))) {
					break;
				}
			}
			if (!m_shield) {
				return true;
			}
		}
		if (!m_through) {
			return true;
		}
		return false;
	}
	public boolean doDown(PointF point)
	{
		if (checkPointOnRect(point.x, point.y)) {
			for (int i = m_controls.size() - 1; i > -1; i--) {
				if (m_controls.get(i).doDown(point)) {
					return true;
				}
				if (DWControlsManager.isMatte(m_controls.get(i))) {
					break;
				}
			}
			if (!m_shield) {
				return true;
			}
		}
		if (!m_through) {
			return true;
		}
		return false;
	}
	
	public boolean doDoubleClick(PointF point)
	{
		if (checkPointOnRect(point.x, point.y)) {
			for (int i = m_controls.size() - 1; i > -1; i--) {
				if (m_controls.get(i).doDoubleClick(point)) {
					return true;
				}
				if (DWControlsManager.isMatte(m_controls.get(i))) {
					break;
				}
			}
			if (!m_shield) {
				return true;
			}
		}
		if (!m_through) {
			return true;
		}
		return false;
	}
	
	public boolean doFling(PointF start, PointF end, float velocityX, float velocityY)
	{
		if (checkPointOnRect(end.x, end.y)) {
			for (int i = m_controls.size() - 1; i > -1; i--) {
				if (m_controls.get(i).doFling(start, end, velocityX, velocityY)) {
					return true;
				}
				if (DWControlsManager.isMatte(m_controls.get(i))) {
					break;
				}
			}
			if (!m_shield) {
				return true;
			}
		}
		if (!m_through) {
			return true;
		}
		return false;
	}
	
	public boolean doDrag(PointF start, PointF end, float velocityX, float velocityY)
	{
		if (checkPointOnRect(end.x, end.y)) {
			if (m_long_listener != null) {
				m_long_listener.OnLong(false);
			}
			for (int i = m_controls.size() - 1; i > -1; i--) {
				if (m_controls.get(i).doDrag(start, end, velocityX, velocityY)) {
					return true;
				}
				if (DWControlsManager.isMatte(m_controls.get(i))) {
					break;
				}
			}
			if (m_drag_listener != null) {
				m_drag_listener.OnDrag(DWControlsManager.getInstance().getDragControl());
			}
			if (!m_shield) {
				return true;
			}
		}
		if (!m_through) {
			return true;
		}
		return false;
	}

	public boolean doScroll(PointF start, PointF end, float distanceX, float distanceY)
	{
		if (checkPointOnRect(start.x, start.y)) {
			for (int i = m_controls.size() - 1; i > -1; i--) {
				if (m_controls.get(i).doScroll(start, end, distanceX, distanceY)) {
					return true;
				}
				if (DWControlsManager.isMatte(m_controls.get(i))) {
					break;
				}
			}
			if (!m_shield) {
				return true;
			}
		}
		if (!m_through) {
			return true;
		}
		return false;
	}

	public boolean onTouch(MotionEvent event, PointF point)
	{
		if (checkPointOnRect(point.x, point.y)) {
			for (int i = m_controls.size() - 1; i > -1; i--) {
				if (m_controls.get(i).onTouch(event, point)) {
					return true;
				}
				if (DWControlsManager.isMatte(m_controls.get(i))) {
					break;
				}
			}
			if (!m_shield) {
				return true;
			}
		}
		if (!m_through) {
			return true;
		}
		return false;
	}
}
